//tapAsync(cb)
//tapPromise()
const { AsyncSeriesWaterfallHook } = require('tapable');

//回调写法 ??? 成功还好，失败有点疑惑
const callback = () => {
	console.time('callback:success:end');
	console.time('callback:error:end');
	const queue = new AsyncSeriesWaterfallHook(['arg1', 'arg2']);

	queue.tapAsync('nodeTask', (arg1, arg2, done) => {
		setTimeout(() => {
			console.log('callback:nodeTask', arg1, arg2);
			done(null, 'result1'); //成功向下
			// done('error', 'result1'); //失败退出
		}, 1000);
	});
	queue.tapAsync('reactTask', (arg1, arg2, done) => {
		setTimeout(() => {
			console.log('callback:reactTask', arg1, arg2);
			done();
		}, 2000);
	});

	queue.callAsync(
		'111',
		'222',
		arg => {
			console.log(arg);
			console.timeEnd('callback:success:end');
		}
		// err => {
		// 	console.timeEnd('callback:error:end', err);
		// } //这个从来没有执行过
	);
};

callback();

//异步带保险串行钩子,所有任务执行完，最后执行成功函数，但是有任何一个结果失败，最后执行失败函数
//Promise写法
const promise = () => {
	console.time('promise:success:end');
	console.time('promise:error:end');
	const queue = new AsyncSeriesWaterfallHook(['arg1', 'arg2']);

	queue.tapPromise('nodeTask', (arg1, arg2) => {
		return new Promise((resolve, reject) => {
			setTimeout(() => {
				console.log('promise:nodeTask', arg1, arg2);
				resolve();
			}, 1000);
		});
	});
	queue.tapPromise('reactTask', (arg1, arg2) => {
		return new Promise((resolve, reject) => {
			setTimeout(() => {
				console.log('promise:reactTask', arg1, arg2);
				reject('error');
			}, 1000);
		});
	});

	queue
		.promise('111', '222')
		.then(() => {
			console.timeEnd('promise:success:end');
		})
		.catch(err => {
			console.timeEnd('promise:error:end');
		});
};

// promise();
